// 题目一
// 计算一致字符串
function countConsistentString(allowed: string, words: string[]): number {
    let sum: number = 0
    words.forEach((wordItem) => {
        const narr = wordItem.split('')
        if (narr.every((item) => allowed.indexOf(item) !== -1)) {
            sum++
        }
    })
    return sum
}
console.log(countConsistentString('abc', ['a', 'b', 'c', 'ab', 'ac', 'bc', 'abc']));

// 题目二
// 获取url及封装深拷贝的方法
function searUrl(url: string) {
    const paramStr = url.split('?')[1]
    const parArr = paramStr.split('&')
    console.log(parArr);

    let obj = {}
    parArr.forEach((item: string) => {
        const key = item.split('=')[0]
        const value = item.split('=')[1]
        const newObj = {
            [key]: value
        }
        console.log(newObj);
        console.log(obj, 'obj');

        obj = {
            ...obj,
            ...newObj
        }
    })
    return obj
}
console.log(searUrl('http://example.com/index.html?a=1&b=2&c=3&d'));

// 第三题
// 买卖股票的最佳时机
function func(prices: number[]) {
    let profit = 0
    for (let i = 1; i < prices.length; i++) {
        let tmp = prices[i] - prices[i - 1]
        if (tmp > 0) {
            profit += tmp
        }
    }
    return profit
}
console.log(func([7, 1, 5, 3, 6, 4]));

// 第四题
// 找只出现了一次的元素
function single(nums: number[]) {
    let num = -1
    for (let i = 0; i < nums.length; i++) {
        if (nums.indexOf(nums[i]) == nums.lastIndexOf(nums[i])) {
            num = nums[i]
        }
    }
    return num
}
console.log(single([1, 2, 1, 2, 4]));
// 多维转一维
function flat<T>(arr: Array<T>, k = 1): Array<T> {
    let narr: Array<T> = []
    arr.forEach((item: T) => {
        if (Array.isArray(item) && k >= 1) {
            narr = narr.concat(flat(item, k - 1))
        } else {
            narr = narr.concat(item)
        }
    })
    return narr
}
console.log(flat([1, [2, 3, 4, [23, 4]]], 1));

